package ph.com.gs3.chat.fragments.view;

import ph.com.gs3.chat.R;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LoginViewFragment extends Fragment {

	public static final String TAG = LoginViewFragment.class.getSimpleName();

	// dummies
	// private String dummyServer = "http://192.168.0.69";
	// private String dummyServer = "http://192.168.1.6";
	// private String dummyServer = "http://192.168.0.103";
	// private String dummyServer = "http://192.168.43.55";
	// private String dummyServer = "http://192.168.0.97";
	private String dummyServer = "http://192.168.0.72";

	// private String dummyEmail = "ervinne.sodusta@gs3.com.ph";
	// private String dummyEmail = "mavie.estacio@gs3.com.ph";
	// private String dummyEmail = "ervinne.dummy@gs3.com.ph";
	private String dummyEmail = "cez.crisostomo@gs3.com.ph";
	private String dummyPassword = "password";

	// {{ Prompts

	public static final String PROMPT_FIELD_IS_REQUIRED = "This field is required";
	public static final String PROMPT_FIELD_INVALID = "Invalid";

	// }}

	// {{ View Fields

	private LinearLayout llFieldsContainer;

	private ProgressBar pbLoginProgress;

	private TextView tvMessage;

	private EditText etServer;
	private EditText etEmail;
	private EditText etPassword;

	private Button bLogin;

	// }}

	private enum FieldsValidity {
		VALID, HAS_ERRORS
	}

	private LoginViewActionListener listener;

	// required empty constructor
	public LoginViewFragment() {}

	// builder method
	public static LoginViewFragment createInstance() {

		// do initialization here

		return new LoginViewFragment();

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			listener = (LoginViewActionListener) activity;
		} catch (ClassCastException e) {
			throw new IllegalStateException(activity.getClass().getSimpleName()
			        + " must implement LoginViewActionListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_login, container, false);

		intializeViews(rootView);

		return rootView;
	}

	private void intializeViews(View rootView) {

		llFieldsContainer = (LinearLayout) rootView.findViewById(R.id.Login_llLoginFieldsContainer);

		pbLoginProgress = (ProgressBar) rootView.findViewById(R.id.Login_pbLoginProgress);

		tvMessage = (TextView) rootView.findViewById(R.id.Login_tvMessage);

		etServer = (EditText) rootView.findViewById(R.id.Login_etServer);
		etEmail = (EditText) rootView.findViewById(R.id.Login_etEmail);
		etPassword = (EditText) rootView.findViewById(R.id.Login_etPassword);

		// defaults:
		etServer.setText(dummyServer);
		etEmail.setText(dummyEmail);
		etPassword.setText(dummyPassword);

		bLogin = (Button) rootView.findViewById(R.id.Login_bLogin);
		bLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				FieldsValidity fieldsValidity = validateAndShowFieldErrors();
				if (fieldsValidity == FieldsValidity.VALID) {

					String server = etServer.getText().toString();
					String email = etEmail.getText().toString();
					String password = etPassword.getText().toString();

					listener.onLoginCommand(server, email, password);
				}

			}
		});

	}

	private FieldsValidity validateAndShowFieldErrors() {

		boolean hasErrors = false;

		String server = etServer.getText().toString();
		String email = etEmail.getText().toString();
		String password = etPassword.getText().toString();

		if (server == null || server.isEmpty()) {
			etServer.setError(PROMPT_FIELD_IS_REQUIRED);
			hasErrors = true;
		}

		if (email == null || email.isEmpty()) {
			etEmail.setError(PROMPT_FIELD_IS_REQUIRED);
			hasErrors = true;
		}

		if (password == null || password.isEmpty()) {
			etPassword.setError(PROMPT_FIELD_IS_REQUIRED);
			hasErrors = true;
		}

		return hasErrors ? FieldsValidity.HAS_ERRORS : FieldsValidity.VALID;

	}

	public void displayErrorMessage(String message) {

		tvMessage.setText(message);
		tvMessage.setTextColor(Color.RED);

	}

	public void displayLoginProgress() {

		tvMessage.setText("");
		llFieldsContainer.setVisibility(View.GONE);
		pbLoginProgress.setVisibility(View.VISIBLE);

	}

	public void displayLoginFields() {

		llFieldsContainer.setVisibility(View.VISIBLE);
		pbLoginProgress.setVisibility(View.GONE);

	}

	public static interface LoginViewActionListener {
		public void onLoginCommand(String server, String email, String password);
	}
}
